package com.linux.huhx.thread3.timer_1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author: huhx
 * @Date: 2017-10-31 上午 10:45
 */
public class Run_1 {
    // 开启了一个线程去调度TimeTask任务，里面有一个TaskQueue。先进先出.
    private static Timer timer = new Timer(true);

    static public class MyTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("Has run now is " + new Date().toLocaleString());
        }
    }

    public static void main(String[] args) {
        try {
            MyTask myTask = new MyTask();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = "2017-10-31 11:01:12";
            Date date = format.parse(dateString);
            System.out.println("now is " + new Date().toLocaleString());
            timer.schedule(myTask, date);
            TimeUnit.SECONDS.sleep(40);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
